﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq;
using SewerAPI.Resources;
using System.Reflection;
using SewerAPI.Util;

namespace SewerAPI.Core
{
    /// <summary>
    /// Message factory to instantiate a message from a string
    /// </summary>
    public class MessageFactory
    {
        /// <summary>
        /// Resolve the message 
        /// </summary>
        /// <param name="strings"></param>
        /// <returns></returns>
        public Message Resolve(Dictionary<string,string> strings)
        {
            try
            {
                BindingFlags _flags = BindingFlags.Public | BindingFlags.Instance;
                Message _m = Activator.CreateInstance(resolveType(strings),
                    _flags, null, new object[] { "" },
                    Configuration.CurrentCulture) as Message;
                
            }
            catch (Exception e) 
            {
                SewerClient.Instance.Log.Error(Strings.ERR03, e);
                return null;
            }
        }



        #region Util Methods

        Type resolveType(Dictionary<string, string> strings)
        {
            try
            {
                var _m = defaultMessage();
                var _attribs = _m.GetExportableAttributes();
                var _typename = _attribs.Where(x => x.IsTypeHint).First().Name;
                return Type.GetType(string.Format("SewerAPI.Core.{0}Message"));
            }
            catch (Exception e)
            {
                Exception _e = new Exception(Strings.ERR02, e);
                throw _e;
            }
        }

        /// <summary>
        /// Return a default message
        /// </summary>
        /// <returns></returns>
        Message defaultMessage()
        {
            return new VoidMessage();
        }

        #endregion
    }
}
